package com.hyy.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hyy.api.domain.customer.CstmCustomerHttpConfig;
import com.hyy.api.domain.customer.bo.CstmCustomerHttpConfigBo;
import com.hyy.api.domain.customer.vo.CstmCustomerHttpConfigVo;
import com.hyy.common.core.utils.MapstructUtils;
import com.hyy.common.core.utils.StringUtils;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hyy.customer.mapper.CstmCustomerHttpConfigMapper;
import com.hyy.customer.service.ICstmCustomerHttpConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 下游客户http配置Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@RequiredArgsConstructor
@Service
public class CstmCustomerHttpConfigServiceImpl implements ICstmCustomerHttpConfigService {

    private final CstmCustomerHttpConfigMapper baseMapper;

    /**
     * 查询下游客户http配置
     *
     * @param id 主键
     * @return 下游客户http配置
     */
    @Override
    public CstmCustomerHttpConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询下游客户http配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 下游客户http配置分页列表
     */
    @Override
    public TableDataInfo<CstmCustomerHttpConfigVo> queryPageList(CstmCustomerHttpConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = buildQueryWrapper(bo);
        Page<CstmCustomerHttpConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的下游客户http配置列表
     *
     * @param bo 查询条件
     * @return 下游客户http配置列表
     */
    @Override
    public List<CstmCustomerHttpConfigVo> queryList(CstmCustomerHttpConfigBo bo) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CstmCustomerHttpConfig> buildQueryWrapper(CstmCustomerHttpConfigBo bo) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CstmCustomerHttpConfig::getId);
        lqw.eq(bo.getCustomerId() != null, CstmCustomerHttpConfig::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getAcceptRate() != null, CstmCustomerHttpConfig::getAcceptRate, bo.getAcceptRate());
        return lqw;
    }

    /**
     * 新增下游客户http配置
     *
     * @param bo 下游客户http配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CstmCustomerHttpConfigBo bo) {
        CstmCustomerHttpConfig add = MapstructUtils.convert(bo, CstmCustomerHttpConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改下游客户http配置
     *
     * @param bo 下游客户http配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CstmCustomerHttpConfigBo bo) {
        CstmCustomerHttpConfig update = MapstructUtils.convert(bo, CstmCustomerHttpConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CstmCustomerHttpConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除下游客户http配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public CstmCustomerHttpConfigVo queryByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerHttpConfig::getCustomerId, customerId);
        List<CstmCustomerHttpConfigVo> list = baseMapper.selectVoList(lqw);
        if(CollUtil.isEmpty(list)) return null;
        return list.get(0);
    }

    @Override
    public void deleteByCustomerId(Long customerId) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerHttpConfig::getCustomerId, customerId);
        baseMapper.delete(lqw);
    }

    @Override
    public void updateByCustomerId(Long customerId, CstmCustomerHttpConfigBo httpConfigBo) {
        LambdaQueryWrapper<CstmCustomerHttpConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(CstmCustomerHttpConfig::getCustomerId, customerId);
        CstmCustomerHttpConfig httpConfig = MapstructUtils.convert(httpConfigBo, CstmCustomerHttpConfig.class);
        baseMapper.update(httpConfig, lqw);


    }
}
